<template>
    <el-dialog class="eldialog" title="详情" :visible.sync="open" width="65%" append-to-body :before-close="closeDelDialog">
        <el-row>
            <el-col :span="24">
                <el-form :model="numInfo" :inline="true">
                    <el-form-item label="任务名称">
                        <el-input v-model="title" disabled placeholder="请输入任务名称" clearable style="width: 240px"
                            @keyup.enter.native="handleQuery" />
                    </el-form-item>

                </el-form>
            </el-col>

        </el-row>

        <queryCriteriaList v-if="listFrom.length > 0" :showSearch="showSearch" :listFrom="listFrom" @resetQuery="resetQuery"
            @handleQuery="handleQuery" ref="queryForm"></queryCriteriaList>
        <el-divider></el-divider>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download"  @click="handleExport">导出</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-printer"  @click="print1Tap()">打印</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus"  @click="onTaskTap()" v-if="rowInfo.JIANDINGSTATE==8">再次鉴定</el-button>
            </el-col>

        </el-row>
        <!-- props: ["tableHeaderList", "tableData", "loading", "tableHeight","isRetrieval"], -->
        <tableList :tableHeaderList="tableHeaderList" :tableData="tableData" :loading="loading" :tableHeight="tableHeight" :iskfjdorbgqx="iskfjdorbgqx"
            @sortChange="sortChange" :isShenjl="true" @handleSelectionChange="handleSelectionChange"></tableList>
        <pagination v-show="total > 0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"
            @pagination="getdatalistbyrenwuidPost" />

    </el-dialog>
</template>
<script>
import { getshowfield, getqueryfieldList, geturl } from "@/api/archive/fileCollection/offlinReception"
import { getjobinfo, getcsfsallnumbyrenwuid, getdatalistbyrenwuid,jiandingagin } from "@/api/archive/archivalAppraisal/classificationIdentify/openArchivesAppraisal"
import { getLastIndexOf } from "@/utils/index.js"
import tableList from "@/components/tableList/index.vue"
import jianDetailsOpen from '@/components/componentsJianDetails/index.vue' //件详情
import juanDetailsOpen from '@/components/componentsJuanDetails/index.vue' //卷详情
import queryCriteriaList from "@/components/queryCriteriaList/index.vue"  //查询列表
import {getTypeObject} from "@/api/archive/dict/dictManageData";
export default {
    props: ['open', 'rowInfo'],
    components: {
        queryCriteriaList,
        tableList,
    },
    data() {
        return {
             // iskfjdorbgqx:0 //:iskfjdorbgqx="0"  开放鉴定   :iskfjdorbgqx="1" 保管期限
            iskfjdorbgqx: '3',
            loading: false,
            listFrom: [], //查询框列表
            // 显示搜索条件
            showSearch: true,
            queryParams: {},
            searchcolumn: [],//查询参数
            numInfo: {
                ALLNUM: undefined,
                CHUSHENNUUM: undefined,
                FUSHENNUUM: undefined
            }, //获取总数/初审分配数/复审分配数
            pageNum: 1,
            pageSize: 10,
            total: 0,
            fields: [],//组装的表头table需要的参数
            tableHeaderList: [],
            tableData: [],
            tableHeight: "400",
            order: undefined,
            openIs: false,
            title: "", //任务名称
            single: true,
            multiple: true,
            ids: [],
            dictList: [],
        }
    },
    watch: {
        rowInfo: {
            handler(valNew) {
                if (valNew) {
                    getjobinfo(valNew.ID).then(res => {
                        this.info = res.data.jobinfo;
                        this.title = res.data.jobinfo.JIANDINGRENWUNAME
                        this.getqueryfieldListPost(); /** 获取查询字段 */
                        this.getshowfieldPost();
                    })
                }
            }, immediate: true
        },
    },
    methods: {

        //多选框选中数据
        handleSelectionChange(ids, single, multiple, selection) {
            console.log(ids, single, multiple, selection)
            this.ids = ids;
            this.single = single;
            this.multiple = multiple;
        },
        /** 获取查询字段 */
        getqueryfieldListPost() {
          var subtablename=this.info.SUB_TABLE_NAME;
          var type=subtablename.substring(subtablename.length-3,subtablename.length-2);
          if(type=='X'){
            subtablename=subtablename.substring(0,subtablename.length-3)+'V'+subtablename.substring(subtablename.length-2);

          }
            getqueryfieldList({
                categoryId: this.info.categoryId,
                menuId: this.$route.meta.menuId,
                subTableName: subtablename
            }).then(response => {
                this.listFrom = response.rows;
            });
        },

        //获取总数/初审分配数/复审分配数
        getcsfsallnumbyrenwuidPost() {
            getcsfsallnumbyrenwuid(this.info.ID).then(res => {
                if (res.data.length > 0) {
                    this.numInfo = res.data[0]
                }

            })
        },

        //获取表头列表
        getshowfieldPost() {
            var level = "";
            if (getLastIndexOf(this.info.SUB_TABLE_NAME) == "F") {  //F 件
                level = "jian"
            } else if (getLastIndexOf(this.info.SUB_TABLE_NAME) == "V") { //卷
                level = "juan"
            } else { //项目
                // level = "xiangmu"
                level = "juan"
            }
            this.loading = true;
            getshowfield({
                categoryId: this.info.categoryId,
                level: level,
                menuId: this.$route.meta.menuId
            }).then(res => {
                this.loading = false;
                this.tableHeaderList = res.data;
                for (var i in res.data) {
                    this.fields.push(res.data[i].FIELD_NAME)
                }
              //获取指定字典列表
              var headerlist = [];
              for (var i = 0; i < this.tableHeaderList.length; i++) {
                if (this.tableHeaderList[i].SHOWTYPE.toString().indexOf('5') != -1) {
                  this.tableHeaderList[i].SHOWTYPE = 5
                }
                if (this.tableHeaderList[i].SHOWTYPE == 5) {
                  headerlist.push(this.tableHeaderList[i].FIELD_NAME)
                }
              }
              if (headerlist.length > 0) {
                getTypeObject(headerlist).then((res) => {
                  this.dictList = res.data;
                })
              }
                this.getdatalistbyrenwuidPost()

            })
        },

        //获取任务单内的档案数据列表
        getdatalistbyrenwuidPost() {
            getdatalistbyrenwuid({
                renwuid: this.info.ID,
                fields: this.fields.toString(),
                menuId: this.$route.meta.menuId,
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                searchcolumn: this.searchcolumn,
                chushenstate: this.chushenstate,//初审状态
                fushenstate: this.fushenstate,//复审状态
                order: this.order
            }).then(res => {

                this.tableData = res.rows;
                this.total = res.total;

            })
        },

        closeDelDialog() {
            this.$emit("closeDelDialog")
        },



        //onChangeTap时间
        onChangeTap(data) {
            var dataArr=""
            if(data.val!=undefined){
              dataArr = data.queryParams;
            }else{
              dataArr = data;
            }
            if (this.queryParams.length > 0) {
                for (var i in this.queryParams) {
                    if (Object.keys(this.queryParams[i]).toString() == Object.keys(data).toString()) {
                        this.queryParams.splice(i, 1)
                    }
                }
                this.queryParams = dataArr;
            } else {
                this.queryParams = dataArr;
            }

        },
        handleQuery(searchcolumn) {
            // var searchcolumn = []
            // for (var k in this.listFrom) {
            //     for (var i in this.queryParams) {
            //         if (this.listFrom[k].FIELD_NAME == i) {
            //             if (this.listFrom[k].SHOWTYPE == 3) {
            //                 searchcolumn.push({
            //                     "queryType": this.listFrom[k].IS_QUERY,
            //                     "showType": this.listFrom[k].SHOWTYPE,
            //                     "column": this.listFrom[k].FIELD_NAME,
            //                     "value": this.queryParams[i][0],
            //                     "valueTwo": this.queryParams[i][1],
            //                 })
            //             } else {
            //                 searchcolumn.push({
            //                     "queryType": this.listFrom[k].IS_QUERY,
            //                     "showType": this.listFrom[k].SHOWTYPE,
            //                     "column": this.listFrom[k].FIELD_NAME,
            //                     "value": this.queryParams[i],
            //
            //                 })
            //             }
            //         }
            //
            //
            //     }
            // }
            //
            // this.searchcolumn = searchcolumn;
            this.searchcolumn = searchcolumn.filter(item => { return item.value !== ''&& item.value !== null});
            this.getdatalistbyrenwuidPost();
        },


        //重置
        resetQuery() {
            this.queryParams = {};
            this.searchcolumn = [];
            this.chushenstate = undefined;
            this.fushenstate = undefined;
            this.getdatalistbyrenwuidPost();
        },
        //排序
        sortChange(val) {
            this.order = val;
            this.getdatalistbyrenwuidPost();
        },

          //导出
          handleExport() {
            console.log(this.rowInfo,this.info)

            if (this.multiple) {
                this.$message({ message: '请选择要导出的数据哦', type: 'warning' })
                return false
            } else {
                var params={
                renwuid:this.info.ID,
                menuId:this.$route.meta.menuId,
                ids:this.ids.toString(),
                order:this.order,
                fields:this.fields.toString()
            }
                this.download('archive/mijiidentify/exportjobda', {
                    ...params
                }, `mijiidentify_${new Date().getTime()}.xlsx`)

            }
        },
                //再次鉴定
                onTaskTap() {
            console.log(this.info, this.rowInfo, "++")


            if (this.multiple) {
                this.$message({ message: '请选择要再次鉴定的数据哦', type: 'warning' })
                return false
            } else {

                var level = "";
                if (getLastIndexOf(this.info.SUB_TABLE_NAME) == "F") {  //F 件
                    level = "f"
                } else if (getLastIndexOf(this.info.SUB_TABLE_NAME) == "V") { //卷
                    level = "v"
                } else { //项目
                    // level = "xiangmu"
                    level = "v"
                }

                jiandingagin({
                    ids: this.ids,
                    level: level,
                    renwuid: this.info.ID,
                }).then(res => {
                    if(res.code==200){
                        this.$modal.msgSuccess(res.msg);
                    }
                })

            }
        },

           //打印
        print1Tap() {
            var data = [];
            this.printList = [];
            if (this.ids != undefined && !this.multiple) {
              getdatalistbyrenwuid({
                renwuid: this.info.ID,
                fields: this.fields.toString(),
                menuId: this.$route.meta.menuId,
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                searchcolumn: this.searchcolumn,
                chushenstate: this.chushenstate,//初审状态
                fushenstate: this.fushenstate,//复审状态
                order: this.order
              }).then(res => {
                var printTableData = res.rows;
                this.getPrintList(printTableData, this.info.SUB_TABLE_NAME)
              })

                // var idsData = this.ids
                // for (var i in this.tableData) {
                //     for (var j in idsData) {
                //         if (this.tableData[i].ID == idsData[j]) {
                //             this.printList.push(this.tableData[i])
                //         }
                //     }
                // }
                // data = { data: this.printList };
                // geturl({
                //     categoryId: this.info.categoryId,
                //     menuId:this.$route.meta.menuId,
                //     type: getLastIndexOf(this.info.SUB_TABLE_NAME),
                //     data: JSON.stringify(data),
                // }).then(res => {
                //     if (res.data.result == "fail") {
                //         this.$message({ message: res.data.msg, type: 'warning' })
                //     } else {
                //         window.open(res.data.url);
                //     }
                //
                //
                // })
            } else {
                this.$message({ message: '请选择要打印的数据哦', type: 'warning' })
            }
        },
        //处理好的打印数据方法
        getPrintList(printTableData, sub_table_name) {
          var that = this;
          var data = [];
          var datalist = ['data']
          this.printList = [];
          var idsData = this.ids
          for (var i in this.tableHeaderList) {
            if (this.tableHeaderList[i].SHOWTYPE == 5) {
              for (var j in printTableData) {
                console.log(printTableData[j])
                for (var x in this.dictList[this.tableHeaderList[i].FIELD_NAME]) {
                  if (printTableData[j][this.tableHeaderList[i].FIELD_NAME] == this.dictList[this.tableHeaderList[i].FIELD_NAME][x].dictValue) {
                    printTableData[j][this.tableHeaderList[i].FIELD_NAME] = this.dictList[this.tableHeaderList[i].FIELD_NAME][x].dictLabel
                  }
                }
              }
            }
          }
          var printList = [];
          for (var c in printTableData) {
            for (var k in idsData) {
              if (printTableData[c].ID == idsData[k]) {
                printList.push(printTableData[c])
              }
            }
          }
          data = [{ data: printList }];
          geturl({
            categoryId: this.info.categoryId,
            menuId:this.$route.meta.menuId,
            type: getLastIndexOf(sub_table_name),
            data: JSON.stringify(data),
            datalist: JSON.stringify(datalist),
          }).then(res => {
            if (res.data.result == "fail") {
              this.$message({ message: res.data.msg, type: 'warning' })
            } else {
              window.open(res.data.url);
            }
          })
        },

        closeDelfindDialog() {
            this.openIs = false;
            this.rowInfo.openIs = false;

        },
    }

}
</script>
<style lang="scss" scoped="scoped" >
.eldialog ::v-deep .el-dialog__body {
    padding: 10px 20px 25px !important;
}

.eldialog ::v-deep .el-dialog {
    // padding: 0 20px 20px 20px !important;
    display: flex;
    flex-direction: column;
    margin: 0 !important;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    max-height: calc(100% - 30px);
    max-width: calc(100% - 30px);
}

.eldialog ::v-deep .el-dialog .el-dialog__body {
    flex: 1;
    overflow-x: hidden;
}

.eldialog ::v-deep ::v-deep .el-form-item__content {
    line-height: 26px;
}

.el-divider {
    margin-top: 0;
}

.eldialog .elinput {
    border: 0 !important
}
</style>
